<?php
/**
 * Created by PhpStorm.
 * TpUser: Laiguiming
 * Date: 2017/6/21 0021
 * Time: 16:52
 */

namespace app\common\controller;
use app\admin\lib\traits\Crud;
use app\admin\model\SysLog;
use think\Controller;

class BackEnd extends Controller
{
    use Crud;

    protected function getCondition($where="",$name="",$alg="=")
    {
        if($alg=="like"){
            if(input($name)){
                $where.=" and ".$name." ".$alg." '%".input($name)."%'";
            };
        }else{
            if(input($name)){
                $where.=" and ".$name." ".$alg." ".input($name);
            };
        }
        return $where;
    }

    //当任何函数加载时候  会调用此函数
    public function _initialize()
    {//默认的方法  会自动执行 特征有点像构造方法
        $uid = session('user')['id'];
        $request = request();
        $login=strpos($request->url(), "login");

        if (empty($uid) && $login==false) {
            return $this->error("请先登录！", U('/admin/index/login'));
        }

        ($this->request->method() == 'GET') ? define('IS_GET',true) : define('IS_GET',false);  //
        ($this->request->method() == 'POST') ? define('IS_POST',true) : define('IS_POST',false);  //

        //组件封装类
        $this->assign('html', new \app\admin\logic\UnitCreate());
    }

    /**
     * 应答失败
     * @param string $msg
     * @return array
     */
    protected function resError($msg = '')
    {
        $res = array("rc" => 1, "msg" => $msg);
        echo json_encode($res);
    }

    /**
     * 应答成功
     * @param string $data
     * @param string $msg
     * @return array
     */
    protected function resSuccess($data = '', $msg = '')
    {
        $res = array("rc" => 0, "data" => $data, "msg" => $msg);
        echo json_encode($res);
    }

    /**
     * 简单应答
     * @param bool $flag
     * @return array
     */
    protected function resData($flag = true)
    {
        if ($flag) {
            return $this->resSuccess();
        } else {
            return $this->resError();
        }
    }

    /**
     * 应答分页数据
     * @param string $data
     * @param int $counts
     * @param int $ps
     */
    protected function resPageData($data = '', $counts = 0, $ps = 10)
    {
        $pages = floor($counts / $ps);
        if ($counts % $ps > 0) {
            $pages++;
        }
        $res = array("rc" => 0, "data" => $data, "pages" => $pages, "total" => $counts);
        echo json_encode($res);
    }

    /**
     * 应答分页
     * @param string $data
     * @param int $counts
     */
    protected function resPage($data = '', $counts = 0)
    {
        $res = array("rows" => $data, "total" => $counts);
        echo json_encode($res);
    }

    /**
     * 保存日志
     * @param $event
     */
    protected function saveLog($event)
    {
        $log = new SysLog();
        $log->user_name = session("user")['user_name'];
        $log->head_img = session("user")['head_img'];
        $log->event = $event;
        $log->save();

        //更新redis
        $data=$log->where("")->order("ct desc")->limit(10)->select();
        cache('sys_logs',$data);
    }
}